package com.yegs.jltqc.serviceImpl;

import com.yegs.jltqc.TO.NamePageQuery;
import com.yegs.jltqc.VO.BaseMenu;
import com.yegs.jltqc.VO.ComplaintItem;
import com.yegs.jltqc.common.StoreException;
import com.yegs.jltqc.dao.BaseMenuRepository;
import com.yegs.jltqc.dao.ComplaintItemRepository;
import com.yegs.jltqc.service.BaseMenuService;
import com.yegs.jltqc.service.ComplaintItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ComplaintItemServiceImpl implements ComplaintItemService {

    @Autowired
    private ComplaintItemRepository complaintItemRepository;

    @Override
    public void save(ComplaintItem complaintItem) throws StoreException{
        if(complaintItem.getId() == null){
            ComplaintItem item = complaintItemRepository.findComplaintItemByInfo(complaintItem.getInfo());
            if(item != null){
                throw new StoreException("投诉内容已存在!");
            }
            complaintItemRepository.save(complaintItem);
        }else{
            ComplaintItem item = complaintItemRepository.getOne(complaintItem.getId());
            if(item == null){
                throw new StoreException("系统错误!");
            }
            item.setInfo(complaintItem.getInfo());
            complaintItemRepository.save(item);
        }

    }

    @Override
    public List<ComplaintItem> list() {
        return complaintItemRepository.findAll();
    }

    @Override
    public ComplaintItem get(Long id) {
        return complaintItemRepository.getOne(id);
    }

    @Override
    public void del(Long id) {
        complaintItemRepository.deleteById(id);
    }

    @Override
    public Page<ComplaintItem> query(NamePageQuery query) {
        PageRequest request = PageRequest.of(query.getPageIndex() - 1, query.getPageSize());
        if(query.getName() == null || query.getName().trim() == ""){
            return complaintItemRepository.findAll(request);
        }
        return complaintItemRepository.findComplaintItemByInfoLike(query.getName(), request);
    }

}


